package im.eg.springdemo2;

import im.eg.springdemo2.stuffs2.AService;
import im.eg.springdemo2.stuffs2.AppConfig2;
import im.eg.springdemo2.stuffs2.CService;
import im.eg.springdemo2.stuffs2.DService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig2.class);
        System.out.println("========================");

        //在初始化阶段检查有与AService这个bean匹配的切面，因该bean没有实现任何接口，所以创建了cglib代理对象
        AService aService = (AService) context.getBean("AService");
        //AService实例是cglib代理对象，他的bService变量为null，因为这个代理对象是在初始化阶段创建的
        System.out.println(aService.getClass());//class im.eg.springdemo2.stuffs2.AService$$EnhancerBySpringCGLIB$$d48abeff
        System.out.println(aService.bService);//null
        aService.test();

        /*DService dService = (DService) context.getBean("DService");
        System.out.println(dService);
        System.out.println(dService.cService);

        System.out.println("====================");
        Iterator<String> it = context.getDefaultListableBeanFactory().getBeanNamesIterator();
        while (it.hasNext())
        {
            System.out.println(it.next());
        }*/

    }
}

//cglib代理对象
class AService$$EnhancerBySpringCGLIB$$xxxx extends AService {
    AService target;

    public AService$$EnhancerBySpringCGLIB$$xxxx(AService aService) {
        target = aService;
    }

    @Override
    public void test() {
        //代理逻辑...
        target.test();
        //代理逻辑...
    }
}

